package com.yex.modules.etcc.controller;

import com.yex.common.utils.PageUtils;
import com.yex.common.utils.R;
import com.yex.common.validator.ValidatorUtils;
import com.yex.common.validator.group.UpdateGroup;
import com.yex.entity.BankListEntity;
import com.yex.entity.OrderBankEntity;
import com.yex.entity.OrderEntity;
import com.yex.service.BankListService;
import com.yex.service.BankService;
import com.yex.service.OrderBankService;
import com.yex.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Little Liu
 * @version V1
 * @date Created in 2018/12/27 16:50
 * @description 描述
 */
@RestController
@RequestMapping("/etcc")
public class C2cController {

    @Autowired
    private BankListService bankListService;
    @Autowired
    private OrderBankService orderBankService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private BankService bankService;

    @RequestMapping("/orderBuy/list")
    public R orderBuyList(@RequestParam Map<String, Object> params) {
        StringBuilder filter = new StringBuilder(" where 1=1 and type=0");
        filter.append(" order by id desc");
        PageUtils page = orderService.queryPage(params, filter.toString());
        return R.ok().put("page", page);
    }

    @RequestMapping("/orderBuy/auditing")
    public R orderBuyAuditing(@RequestBody Long[] userIds) {
        for (Long userId : userIds) {
            OrderEntity orderEntity = orderService.selectById(userId);
            if (orderEntity != null && orderEntity.getStatus() == 1) {
                orderService.updateOrderAuditing(orderEntity.getId());
            }
        }
        return R.ok();
    }

    @RequestMapping("/orderBuy/noAuditing")
    public R orderBuyNoAuditing(@RequestBody Long[] userIds) {
        for (Long userId : userIds) {
            OrderEntity orderEntity = orderService.selectById(userId);
            if (orderEntity != null && orderEntity.getStatus() == 1) {
                orderService.updateOrderNoAuditing(orderEntity.getId());
            }
        }
        return R.ok();
    }


    @RequestMapping("/orderSell/list")
    public R orderSellList(@RequestParam Map<String, Object> params) {
        StringBuilder filter = new StringBuilder(" where 1=1 and type=1");
        filter.append(" order by id desc");
        PageUtils page = orderService.queryPage(params, filter.toString());
        return R.ok().put("page", page);
    }


    @RequestMapping("/bank/list")
    public R bankList(@RequestParam Map<String, Object> params) {
        StringBuilder filter = new StringBuilder(" where 1=1 ");
        filter.append(" order by id desc");
        PageUtils page = bankListService.queryPage(params, filter.toString());
        return R.ok().put("page", page);
    }

    @RequestMapping("/bank/listTree")
    public List<BankListEntity> bankListTree(@RequestParam Map<String, Object> params) {
        StringBuilder filter = new StringBuilder(" where 1=1 ");
        filter.append(" order by id desc");
        PageUtils page = bankListService.queryPage(params, filter.toString());
        return (List<BankListEntity>) page.getList();
    }

    @RequestMapping("/bank/save")
    public R bankSave(@RequestBody BankListEntity bankListEntity) {
        if (StringUtils.isBlank(bankListEntity.getTitle())) {
            return R.error("不能为空");
        }
        bankListEntity.setStatus(1);
        bankListEntity.setCreateTime(new Date());
        bankListEntity.setUpdateTime(new Date());
        bankListService.insert(bankListEntity);
        return R.ok();
    }


    @RequestMapping("/orderBank/list")
    public R orderBankList(@RequestParam Map<String, Object> params) {
        StringBuilder filter = new StringBuilder(" where 1=1 ");
        filter.append(" order by id desc");
        PageUtils page = orderBankService.queryPage(params, filter.toString());
        List<OrderBankEntity> list = (List<OrderBankEntity>) page.getList();
        for (OrderBankEntity orderBankEntity : list) {
            BankListEntity bankListEntity = bankListService.selectById(orderBankEntity.getBank());
            orderBankEntity.setBankName(bankListEntity.getTitle());
        }
        return R.ok().put("page", page);
    }

    @RequestMapping("/orderBank/info/{id}")
    public R info(@PathVariable("id") Long id) {
        OrderBankEntity orderBankEntity = orderBankService.selectById(id);
        return R.ok().put("orderBank", orderBankEntity);
    }

    @RequestMapping("/orderBank/save")
    public R orderBankSave(@RequestBody OrderBankEntity orderBankEntity) {
        if (StringUtils.isBlank(orderBankEntity.getAddress())
                || StringUtils.isBlank(orderBankEntity.getBranchAddress())
                || StringUtils.isBlank(orderBankEntity.getCardNumber())
                || StringUtils.isBlank(orderBankEntity.getName())
                || orderBankEntity.getBank() == null
                ) {
            return R.error("不能为空");
        }
        Map<String, Object> map = new HashMap<>(2);
        map.put("status", "1");
        List<OrderBankEntity> orderBankEntityList = orderBankService.selectByMap(map);
        if (orderBankEntityList.size() >= 1) {
            return R.error("已经配置了");
        }
        orderBankEntity.setStatus(1);
        orderBankEntity.setCreateTime(new Date());
        orderBankEntity.setUpdateTime(new Date());
        orderBankService.insert(orderBankEntity);
        return R.ok();
    }

    @RequestMapping("/orderBank/update")
    public R update(@RequestBody OrderBankEntity orderBankEntity) {
        ValidatorUtils.validateEntity(orderBankEntity, UpdateGroup.class);

        if (StringUtils.isBlank(orderBankEntity.getAddress())
                || StringUtils.isBlank(orderBankEntity.getBranchAddress())
                || StringUtils.isBlank(orderBankEntity.getCardNumber())
                || StringUtils.isBlank(orderBankEntity.getName())
                || orderBankEntity.getBank() == null
                ) {
            return R.error("不能为空");
        }
        orderBankEntity.setUpdateTime(new Date());
        orderBankService.updateById(orderBankEntity);
        return R.ok();
    }


    @RequestMapping("/userBank/list")
    public R userBankList(@RequestParam Map<String, Object> params) {
        StringBuilder filter = new StringBuilder(" where 1=1 and b.status=1");
        String mobile = (String) params.get("mobile");
        if (!StringUtils.isBlank(mobile)) {
            filter.append(" and (u.mobile='" + mobile + "' or b.user_id='" + mobile + "')");
        }
        filter.append(" order by b.id desc");
        PageUtils page = bankService.queryPage(params, filter.toString());
        return R.ok().put("page", page);
    }


}
